package com.atguigu.mr.flow;


import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @description:
 * @create: 2019-12-15 22:34
 * @author: baojinlong
 **/
@Slf4j
public class FlowSumDriver {
    public static void main(String[] args) throws Exception {
        // 测试阶段可以直接将路径配置固定写在代码中
        String[] testPath = new String[]{"D:/home/test/big-data/input1", "D:/home/test/big-data/output4"};
        Configuration configuration = new Configuration();
        // 1.获取job对象
        Job job = Job.getInstance(configuration);
        // 2.关联jar
        job.setJarByClass(FlowSumDriver.class);
        job.setMapperClass(FlowCountMapper.class);
        job.setReducerClass(FlowCountReducer.class);
        // 3.设置输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        //指定自定义数据分区
        job.setPartitionerClass(ProvincePartitioner.class);
        // 必须指定相应数量的reduce task,分区个数和reduce 1:1,reduceTasks个数不能小于分区个数,设置1则就一个分区,如果2,3,4则报错.设置6则有空分区
        job.setNumReduceTasks(5);


        // 4.设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(testPath[0]));
        FileOutputFormat.setOutputPath(job, new Path(testPath[1]));
        // 5.提交job
        boolean result = job.waitForCompletion(true);
        log.info("计算结果是:{}", result);
        System.exit(result ? 0 : 1);
    }
}
